WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
SE2.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21#include "core/common/wolf.h"
22#include "core/math/rotations.h"
24
25#include <Eigen/Geometry>
26#include <Eigen/Dense>
27
28/*
29 * The functions in this file are related to manipulations of Delta motion magnitudes used in 2d motion.
30 *
31 * The Delta is defined as a simple 2d pose with the rotation expressed as an angle,
32 * Delta = [Dp, Do]
33 * with
34 * Dp : position delta
35 * Do : angle delta (always in (-pi, pi])
36 * and its tangent vector (Lie algebra) is tau = [rho, theta].
37 *
38 * The functions are listed below:
39 *
40 * - identity: I = Delta at the origin, with Dp = [0,0]; Do = 0, so that D (+) I = I (+) D = D
41 * - inverse: so that D (+) D.inv = D.inv (+) D = I
42 * - compose: Dc = D1 (+) D2
43 * - between: Db = D2 (-) D1 := D1.inv (+) D2, so that D2 = D1 (+) Db
44 * - exp: go from tangent space to delta manifold (SE(2) exponential map)
45 * - log: go from delta manifold to tangent space (SE(2) logarithmic map)
46 * - plus: block-wise: D2 = [Dp1 + dp, Do1 + do]
47 * - minus: block-wise: d = [Dp2 - Dp1, Do2 - Do1], so that D2 = plus(D1, d)
48 * - interpolate: D = D1 (+) exp( t * log( D1.inv (+) D2 ) )
49 * - toSE2: 3x3 homogeneous matrix
50 * - jac_SE2_left, jac_SE2_right, jac_SE2_left_inv, jac_SE2_right_inv: Jacobians of SE(2)
51 *
52 * Each function is provided (when applicable) with the following APIs:
53 * - by parts, with the rotation as a scalar angle
54 * - by parts, with the rotation as a vector (1-vector with the angle)
55 * - full 3-vector [p, o] as output parameter and as return value
56 * - full 3-vector with Jacobians. Jacobians are block-wise: w.r.t. perturbations [dp, do] with additive dp
57 * and do, i.e. consistent with plus() and minus().
58 * - VectorComposite with keys 'P' and 'O', as output parameter and as return value
59 *
60 * ------------------------------------------------------------------------------------------
61 *
62 * Some of the functions below are based on:
63 *
64 * [1] J. Sola et. al. "A micro Lie theory for state estimation in robotics", tech rep. IRI 2018,
65 * https://arxiv.org/pdf/1812.01537.pdf
66 */
67
68namespace wolf
69{
70namespace SO2
71{
72using namespace Eigen;
73
76template <typename T>
77inline Matrix<T, 2, 2> exp(const T theta)
78{
79 return Rotation2D<T>(theta).matrix();
80}
81
82} // namespace SO2
83
84namespace SE2
85{
86using namespace Eigen;
87
90template <class T>
91inline Matrix<T, 2, 2> skew(const T& t)
92{
93 return (Matrix<T, 2, 2>() << (T)0, -t, t, (T)0).finished();
94}
95
98template <class D>
99inline Matrix<typename D::Scalar, 2, 1> skewed(const MatrixBase<D>& t)
100{
101 return Matrix<typename D::Scalar, 2, 1>(-t(1), t(0));
102}
103
106template <typename T>
107inline Matrix<T, 2, 2> V_helper(const T theta)
108{
109 using std::abs;
110 T s; // sin(theta) / theta
111 T c_1; // (1-cos(theta)) / theta
112
113 if (abs(theta) > T(Constants::EPS))
114 {
115 // [1] eq. 158, with 1 - cos(theta) = 2 sin^2(theta/2) (numerically stable)
116 T sin_half = sin(theta / T(2.0));
117 s = sin(theta) / theta;
118 c_1 = T(2.0) * sin_half * sin_half / theta;
119 }
120 else
121 {
122 // approx of [1] eq. 158
123 s = T(1.0) - theta * theta / T(6.0); // sin(x) ~= x - x^3/6
124 c_1 = theta / T(2.0); // cos(x) ~= 1 - x^2/2
125 }
126
127 return (Matrix<T, 2, 2>() << s, -c_1, c_1, s).finished();
128}
129
132template <typename T>
133inline Matrix<T, 2, 2> V_inv_helper(const T theta)
134{
135 using std::abs;
136 T a; // theta * sin(theta) / (2 * (1 - cos(theta))) = (theta / 2) / tan(theta / 2)
137
138 if (abs(theta) > T(Constants::EPS))
139 a = (theta / T(2.0)) / tan(theta / T(2.0));
140 else
141 a = T(1.0) - theta * theta / T(12.0);
142
143 return (Matrix<T, 2, 2>() << a, theta / T(2.0), -theta / T(2.0), a).finished();
144}
145
148template <class D, typename T>
149inline Matrix<T, 2, 1> J_Vp_theta(const MatrixBase<D>& p, const T theta)
150{
152
153 /* Jacobian of t = V(theta)*p wrt theta, with V = [s, -c_1; c_1, s]:
154 * dV/dtheta = [ds, -dc_1; dc_1, ds]
155 * ds = d(sin(theta)/theta)/dtheta = (theta cos(theta) - sin(theta)) / theta^2
156 * dc_1 = d((1-cos(theta))/theta)/dtheta = (theta sin(theta) - 1 + cos(theta)) / theta^2
157 * (Taylor series for small angles, where the closed forms are numerically unstable)
158 */
159 using std::abs;
160 T ds, dc_1;
161 if (abs(theta) > T(1e-2))
162 {
163 T s_th = sin(theta);
164 T c_th = cos(theta);
165 T theta2 = theta * theta;
166 ds = (theta * c_th - s_th) / theta2;
167 dc_1 = (theta * s_th - T(1.0) + c_th) / theta2;
168 }
169 else
170 {
171 T theta2 = theta * theta;
172 ds = theta * (-T(1. / 3.) + theta2 * (T(1. / 30.) - theta2 / T(840.)));
173 dc_1 = T(0.5) + theta2 * (-T(1. / 8.) + theta2 * (T(1. / 144.) - theta2 / T(5760.)));
174 }
175
176 return Matrix<T, 2, 1>(ds * p(0) - dc_1 * p(1), dc_1 * p(0) + ds * p(1));
177}
178
180// JACOBIANS OF SE(2)
181
184template <typename D>
185inline Matrix<typename D::Scalar, 3, 3> jac_SE2_left(const MatrixBase<D>& tangent)
186{
188 typedef typename D::Scalar T;
189
190 T x = tangent(0);
191 T y = tangent(1);
192 T theta = tangent(2);
193
194 /* third column: [x B + y A, -x A + y B] with
195 * A = (1 - cos(theta)) / theta^2
196 * B = (theta - sin(theta)) / theta^2
197 * (Taylor series for small angles, where the closed forms are numerically unstable)
198 */
199 using std::abs;
200 T A, B;
201 if (abs(theta) > T(1e-2))
202 {
203 T sin_half = sin(theta / T(2.0));
204 T theta2 = theta * theta;
205 A = T(2.0) * sin_half * sin_half / theta2;
206 B = (theta - sin(theta)) / theta2;
207 }
208 else
209 {
210 T theta2 = theta * theta;
211 A = T(0.5) + theta2 * (-T(1. / 24.) + theta2 / T(720.));
212 B = theta * (T(1. / 6.) + theta2 * (-T(1. / 120.) + theta2 / T(5040.)));
213 }
214
215 Matrix<T, 3, 3> Jl;
216 Jl.template topLeftCorner<2, 2>() = V_helper(theta);
217 Jl(0, 2) = x * B + y * A;
218 Jl(1, 2) = -x * A + y * B;
219 Jl(2, 0) = T(0);
220 Jl(2, 1) = T(0);
221 Jl(2, 2) = T(1);
222 return Jl;
223}
224
227template <typename D>
228inline Matrix<typename D::Scalar, 3, 3> jac_SE2_right(const MatrixBase<D>& tangent)
229{
230 return jac_SE2_left(Matrix<typename D::Scalar, 3, 1>(-tangent));
231}
232
235template <typename D>
236inline Matrix<typename D::Scalar, 3, 3> jac_SE2_left_inv(const MatrixBase<D>& tangent)
237{
238 typedef typename D::Scalar T;
239
240 // Jl = [A, b; 0, 1] --> Jl.inv = [A.inv, -A.inv * b; 0, 1]
241 Matrix<T, 3, 3> Jl = jac_SE2_left(tangent);
242 Matrix<T, 2, 2> A_inv = V_inv_helper(T(tangent(2)));
243 Matrix<T, 3, 3> Jl_inv = Matrix<T, 3, 3>::Identity();
244 Jl_inv.template topLeftCorner<2, 2>() = A_inv;
245 Jl_inv.template topRightCorner<2, 1>() = -A_inv * Jl.template topRightCorner<2, 1>();
246 return Jl_inv;
247}
248
251template <typename D>
252inline Matrix<typename D::Scalar, 3, 3> jac_SE2_right_inv(const MatrixBase<D>& tangent)
253{
254 return jac_SE2_left_inv(Matrix<typename D::Scalar, 3, 1>(-tangent));
255}
256
258// IDENTITY
259
262template <typename D>
263inline void identity(MatrixBase<D>& p, typename D::Scalar& o)
264{
266 p.setZero();
267 o = typename D::Scalar(0);
268}
269
272template <typename D1, typename D2>
273inline void identity(MatrixBase<D1>& p, MatrixBase<D2>& o)
274{
276 identity(p, o(0));
277}
278
281template <typename D>
282inline void identity(MatrixBase<D>& d)
283{
285
286 Map<Matrix<typename D::Scalar, 2, 1>> p(&d(0));
287 identity(p, d(2));
288}
289
292template <typename T = double>
293inline Matrix<T, 3, 1> identity()
294{
295 Matrix<T, 3, 1> ret;
296 identity(ret);
297 return ret;
298}
299
303{
304 v.clear();
305 v['P'].resize(2);
306 v['O'].resize(1);
307 identity(v['P'], v['O']);
308}
309
313{
315 identity(v);
316 return v;
317}
318
320// INVERSE
321
324template <typename D1, typename D2>
325inline void inverse(const MatrixBase<D1>& p, const typename D1::Scalar& o, MatrixBase<D2>& ip, typename D2::Scalar& io)
326{
329
330 ip = SO2::exp(-o) * -p;
331 io = pi2pi(-o);
332}
333
336template <typename D1, typename D2, typename D3, typename D4>
337inline void inverse(const MatrixBase<D1>& p, const MatrixBase<D2>& o, MatrixBase<D3>& ip, MatrixBase<D4>& io)
338{
341
342 inverse(p, o(0), ip, io(0));
343}
344
347template <typename D1, typename D2>
348inline void inverse(const MatrixBase<D1>& d, MatrixBase<D2>& id)
349{
352
353 const auto p = d.template head<2>();
354 Map<Matrix<typename D2::Scalar, 2, 1>> ip(&id(0));
355
356 inverse(p, d(2), ip, id(2));
357}
358
361template <typename D>
362inline Matrix<typename D::Scalar, 3, 1> inverse(const MatrixBase<D>& d)
363{
364 Matrix<typename D::Scalar, 3, 1> id;
365 inverse(d, id);
366 return id;
367}
368
371inline void inverse(const VectorComposite& v, VectorComposite& c)
372{
373 c['P'].resize(2);
374 c['O'].resize(1);
375 inverse(v.at('P'), v.at('O'), c['P'], c['O']);
376}
377
381{
383 inverse(v, c);
384 return c;
385}
386
388// COMPOSE
389
392template <typename D1, typename D2, typename D3>
393inline void compose(const MatrixBase<D1>& p1,
394 const typename D1::Scalar& o1,
395 const MatrixBase<D2>& p2,
396 const typename D2::Scalar& o2,
397 MatrixBase<D3>& pc,
398 typename D3::Scalar& oc)
399{
403
404 // tc = t1 + R1 t2
405 pc = p1 + SO2::exp(o1) * p2;
406
407 // Rc = R1 R2 --> theta = theta1 + theta2
408 oc = pi2pi(o1 + o2);
409}
410
413template <typename D1, typename D2, typename D4, typename D5, typename D7, typename D8>
414inline void compose(const MatrixBase<D1>& p1,
415 const MatrixBase<D2>& o1,
416 const MatrixBase<D4>& p2,
417 const MatrixBase<D5>& o2,
418 MatrixBase<D7>& pc,
419 MatrixBase<D8>& oc)
420{
424
425 compose(p1, o1(0), p2, o2(0), pc, oc(0));
426}
427
430template <class D1, class D2, class D3>
431inline void compose(const MatrixBase<D1>& _delta1,
432 const MatrixBase<D2>& _delta2,
433 MatrixBase<D3>& _delta1_compose_delta2)
434{
437 MatrixSizeCheck<3, 1>::check(_delta1_compose_delta2);
438
439 const auto p1 = _delta1.template head<2>();
440 const auto p2 = _delta2.template head<2>();
441 Map<Matrix<typename D3::Scalar, 2, 1>> pc(&_delta1_compose_delta2(0));
442
443 compose(p1, _delta1(2), p2, _delta2(2), pc, _delta1_compose_delta2(2));
444}
445
448template <class D1, class D2>
449inline Matrix<typename D1::Scalar, 3, 1> compose(const MatrixBase<D1>& _delta1, const MatrixBase<D2>& _delta2)
450{
451 Matrix<typename D1::Scalar, 3, 1> delta1_compose_delta2;
452 compose(_delta1, _delta2, delta1_compose_delta2);
453 return delta1_compose_delta2;
454}
455
458template <class D1, class D2, class D3, class D4, class D5>
459inline void compose(const MatrixBase<D1>& _delta1,
460 const MatrixBase<D2>& _delta2,
461 MatrixBase<D3>& _delta1_compose_delta2,
462 MatrixBase<D4>& _J_compose_delta1,
463 MatrixBase<D5>& _J_compose_delta2)
464{
467 MatrixSizeCheck<3, 1>::check(_delta1_compose_delta2);
468 MatrixSizeCheck<3, 3>::check(_J_compose_delta1);
469 MatrixSizeCheck<3, 3>::check(_J_compose_delta2);
470
471 typedef typename D1::Scalar T;
472
473 Matrix<T, 2, 2> R1 = SO2::exp(T(_delta1(2))); // need temporary
474
475 /* Jacobians in composite form [1] see eqs. (89, 125, 129, 130)
476 *
477 * resulting delta is:
478 *
479 * tc = t1 + R1 t2 (*)
480 * Rc = R1 R2 (**)
481 *
482 * Jacobians have the form:
483 *
484 * [ J_t_t J_t_R ]
485 * [ J_r_t J_R_R ]
486 *
487 * Jacobian blocks are:
488 *
489 * J_tc_t1 = I trivial from (*)
490 * J_tc_R1 = R1 [1]x t2 = R1 (-t2.y ; t2.x) see [1]: eq. (129)
491 *
492 * J_Rc_t1 = (0 0) trivial from (**)
493 * J_Rc_R1 = 1 see [1]: eq. (125)
494 *
495 * J_tc_t2 = R1 see [1]: eq. (130)
496 * J_tc_R2 = 0 trivial from (*)
497 *
498 * J_Rc_t2 = 0 trivial from (**)
499 * J_Rc_R2 = 1 see [1]: eq. (125)
500 */
501
502 _J_compose_delta1.setIdentity();
503 _J_compose_delta1.template block<2, 1>(0, 2) = R1 * skewed(_delta2.template head<2>());
504
505 _J_compose_delta2.setIdentity(3, 3);
506 _J_compose_delta2.template block<2, 2>(0, 0) = R1;
507
508 // compose deltas -- done here to avoid aliasing when calling with input `_delta1` and result referencing the same
509 // variable
510 compose(_delta1, _delta2, _delta1_compose_delta2);
511}
512
515inline void compose(const VectorComposite& _x1, const VectorComposite& _x2, VectorComposite& _c)
516{
517 _c['P'].resize(2);
518 _c['O'].resize(1);
519 compose(_x1.at('P'), _x1.at('O'), _x2.at('P'), _x2.at('O'), _c['P'], _c['O']);
520}
521
525{
527 compose(x1, x2, c);
528 return c;
529}
530
531// inline void compose(const VectorComposite& _x1,
532// const VectorComposite& _x2,
533// VectorComposite& _c,
534// MatrixComposite& _J_c_x1,
535// MatrixComposite& _J_c_x2)
536// {
537// const auto& p1 = _x1.at('P');
538// const auto& a1 = _x1.at('O')(0); // angle
539// Matrix2d R1 = SO2::exp(a1); // need temporary
540
541// const auto& p2 = _x2.at('P');
542// const auto& a2 = _x2.at('O')(0); // angle
543
544// /* Jacobians in composite form [1] see eqs. (89, 125, 129, 130)
545// * (see compose() above)
546// */
547
548// _J_c_x1.clear();
549// _J_c_x1.emplace('P', 'P', Matrix2d::Identity());
550// _J_c_x1.emplace('P', 'O', MatrixXd(R1 * skewed(p2)));
551// _J_c_x1.emplace('O', 'P', RowVector2d(0, 0));
552// _J_c_x1.emplace('O', 'O', Matrix1d(1));
553
554// _J_c_x2.clear();
555// _J_c_x2.emplace('P', 'P', R1);
556// _J_c_x2.emplace('P', 'O', Vector2d(0, 0));
557// _J_c_x2.emplace('O', 'P', RowVector2d(0, 0));
558// _J_c_x2.emplace('O', 'O', Matrix1d(1));
559
560// // Actually compose the vectors here. This avoids aliasing in case this is called with 'c' coinciding with 'x1'
561// or
562// // 'x2'.
563
564// // tc = t1 + R1 t2
565// _c['P'] = p1 + R1 * p2;
566
567// // Rc = R1 R2 --> theta = theta1 + theta2
568// _c['O'] = Matrix1d(pi2pi(a1 + a2));
569// }
570
572// BETWEEN
573
576template <typename D1, typename D2, typename D3>
577inline void between(const MatrixBase<D1>& p1,
578 const typename D1::Scalar& o1,
579 const MatrixBase<D2>& p2,
580 const typename D2::Scalar& o2,
581 MatrixBase<D3>& p12,
582 typename D3::Scalar& o12)
583{
587
588 p12 = SO2::exp(-o1) * (p2 - p1);
589 o12 = pi2pi(o2 - o1);
590}
591
594template <typename D1, typename D2, typename D4, typename D5, typename D7, typename D8>
595inline void between(const MatrixBase<D1>& p1,
596 const MatrixBase<D2>& o1,
597 const MatrixBase<D4>& p2,
598 const MatrixBase<D5>& o2,
599 MatrixBase<D7>& p12,
600 MatrixBase<D8>& o12)
601{
605
606 between(p1, o1(0), p2, o2(0), p12, o12(0));
607}
608
611template <typename D1, typename D2, typename D3>
612inline void between(const MatrixBase<D1>& d1, const MatrixBase<D2>& d2, MatrixBase<D3>& d12)
613{
617
618 const auto p1 = d1.template head<2>();
619 const auto p2 = d2.template head<2>();
620 Map<Matrix<typename D3::Scalar, 2, 1>> p12(&d12(0));
621
622 between(p1, d1(2), p2, d2(2), p12, d12(2));
623}
624
627template <typename D1, typename D2>
628inline Matrix<typename D1::Scalar, 3, 1> between(const MatrixBase<D1>& d1, const MatrixBase<D2>& d2)
629{
630 Matrix<typename D1::Scalar, 3, 1> d12;
631 between(d1, d2, d12);
632 return d12;
633}
634
637inline void between(const VectorComposite& x1, const VectorComposite& x2, VectorComposite& x12)
638{
639 x12['P'].resize(2);
640 x12['O'].resize(1);
641 between(x1.at('P'), x1.at('O'), x2.at('P'), x2.at('O'), x12['P'], x12['O']);
642}
643
647{
648 VectorComposite x12;
649 between(x1, x2, x12);
650 return x12;
651}
652
654// EXP
655
658template <typename D1, typename D2>
659inline void exp(const MatrixBase<D1>& rho, const typename D1::Scalar& theta, MatrixBase<D2>& p, typename D2::Scalar& o)
660{
663
664 // [1] eq. 156
665 p = V_helper(theta) * rho;
666 o = pi2pi(theta);
667}
668
671template <typename D1, typename D2, typename D3, typename D4>
672inline void exp(const MatrixBase<D1>& rho, const MatrixBase<D2>& theta, MatrixBase<D3>& p, MatrixBase<D4>& o)
673{
676
677 exp(rho, theta(0), p, o(0));
678}
679
682template <class D1, class D2>
683inline void exp(const MatrixBase<D1>& _tau, MatrixBase<D2>& _delta)
684{
687
688 const auto rho = _tau.template head<2>();
689 Map<Matrix<typename D2::Scalar, 2, 1>> p(&_delta(0));
690
691 exp(rho, _tau(2), p, _delta(2));
692}
693
696template <typename D>
697inline Matrix<typename D::Scalar, 3, 1> exp(const MatrixBase<D>& _tau)
698{
699 Matrix<typename D::Scalar, 3, 1> _delta;
700 exp(_tau, _delta);
701 return _delta;
702}
703
706template <class D1, class D2, class D3>
707inline void exp(const MatrixBase<D1>& _tau, MatrixBase<D2>& _delta, MatrixBase<D3>& _J_delta_tau)
708{
711 MatrixSizeCheck<3, 3>::check(_J_delta_tau);
712
713 typedef typename D1::Scalar T;
714
715 Matrix<T, 3, 1> tau = _tau; // copy (tau and delta may alias)
716
717 // Jacobian is the composite definition [1] eq. 89, with jacobian blocks:
718 // J_Vp_p = V: see V_helper, eq. 158
719 // J_Vp_theta: see fcn helper
720 // J_theta_theta = 1; eq. 126
721 _J_delta_tau.template topLeftCorner<2, 2>() = V_helper(tau(2));
722 _J_delta_tau.template topRightCorner<2, 1>() = J_Vp_theta(tau.template head<2>(), tau(2));
723 _J_delta_tau.template bottomRows<1>() << T(0), T(0), T(1);
724
725 exp(tau, _delta);
726}
727
730inline void exp(const VectorComposite& _tau, VectorComposite& _delta)
731{
732 _delta['P'].resize(2);
733 _delta['O'].resize(1);
734 exp(_tau.at('P'), _tau.at('O'), _delta['P'], _delta['O']);
735}
736
740{
741 VectorComposite res;
742 exp(tau, res);
743 return res;
744}
745
746// inline void exp(const VectorComposite& _tau, VectorComposite& _delta, MatrixComposite& _J_delta_tau)
747// {
748// // [1] eq. 156
749// const auto& p = _tau.at('P');
750// const auto& theta = pi2pi(_tau.at('O')(0));
751// Matrix2d V = V_helper(theta);
752
753// _delta['P'] = V * p;
754// _delta['O'] = Matrix1d(theta);
755
756// // Jacobian follows the composite definition in [1] eq. 89, with jacobian blocks:
757// // J_Vp_p = V: see V_helper, eq. 158
758// // J_Vp_theta: see fcn helper
759// // J_theta_theta = 1; eq. 126
760// _J_delta_tau.clear();
761// _J_delta_tau.emplace('P', 'P', V);
762// _J_delta_tau.emplace('P', 'O', J_Vp_theta(p, theta));
763// _J_delta_tau.emplace('O', 'P', RowVector2d(0.0, 0.0));
764// _J_delta_tau.emplace('O', 'O', Matrix1d(1));
765// }
766
768// LOG
769
772template <typename D1, typename D2>
773inline void log(const MatrixBase<D1>& p, const typename D1::Scalar& o, MatrixBase<D2>& rho, typename D2::Scalar& theta)
774{
777
778 typename D1::Scalar th = pi2pi(o);
779 rho = V_inv_helper(th) * p;
780 theta = th;
781}
782
785template <typename D1, typename D2, typename D3, typename D4>
786inline void log(const MatrixBase<D1>& p, const MatrixBase<D2>& o, MatrixBase<D3>& rho, MatrixBase<D4>& theta)
787{
790
791 log(p, o(0), rho, theta(0));
792}
793
796template <class D1, class D2>
797inline void log(const MatrixBase<D1>& _delta, MatrixBase<D2>& _tau)
798{
801
802 const auto p = _delta.template head<2>();
803 Map<Matrix<typename D2::Scalar, 2, 1>> rho(&_tau(0));
804
805 log(p, _delta(2), rho, _tau(2));
806}
807
810template <typename D>
811inline Matrix<typename D::Scalar, 3, 1> log(const MatrixBase<D>& _delta)
812{
813 Matrix<typename D::Scalar, 3, 1> _tau;
814 log(_delta, _tau);
815 return _tau;
816}
817
820template <class D1, class D2, class D3>
821inline void log(const MatrixBase<D1>& _delta, MatrixBase<D2>& _tau, MatrixBase<D3>& _J_tau_delta)
822{
825 MatrixSizeCheck<3, 3>::check(_J_tau_delta);
826
827 typedef typename D1::Scalar T;
828
829 Matrix<T, 3, 1> tau;
830 log(_delta, tau);
831
832 // inverse of the jacobian of exp(): J_exp = [V, J_Vp_theta; 0, 1] --> J_log = [V.inv, -V.inv * J_Vp_theta; 0, 1]
833 Matrix<T, 2, 2> V_inv = V_inv_helper(tau(2));
834
835 _J_tau_delta.template topLeftCorner<2, 2>() = V_inv;
836 _J_tau_delta.template topRightCorner<2, 1>() = -V_inv * J_Vp_theta(tau.template head<2>(), tau(2));
837 _J_tau_delta.template bottomRows<1>() << T(0), T(0), T(1);
838
839 _tau = tau;
840}
841
844inline void log(const VectorComposite& _delta, VectorComposite& _tau)
845{
846 _tau['P'].resize(2);
847 _tau['O'].resize(1);
848 log(_delta.at('P'), _delta.at('O'), _tau['P'], _tau['O']);
849}
850
854{
855 VectorComposite tau;
856 log(delta, tau);
857 return tau;
858}
859
861// PLUS (block-wise)
862
865template <typename D1, typename D2, typename D3>
866inline void plus(const MatrixBase<D1>& p1,
867 const typename D1::Scalar& o1,
868 const MatrixBase<D2>& p2,
869 const typename D2::Scalar& o2,
870 MatrixBase<D3>& plus_p,
871 typename D3::Scalar& plus_o)
872{
876
877 plus_p = p1 + p2;
878 plus_o = pi2pi(o1 + o2);
879}
880
883template <typename D1, typename D2, typename D4, typename D5, typename D7, typename D8>
884inline void plus(const MatrixBase<D1>& p1,
885 const MatrixBase<D2>& o1,
886 const MatrixBase<D4>& p2,
887 const MatrixBase<D5>& o2,
888 MatrixBase<D7>& plus_p,
889 MatrixBase<D8>& plus_o)
890{
894
895 plus(p1, o1(0), p2, o2(0), plus_p, plus_o(0));
896}
897
900template <typename D1, typename D2, typename D3>
901inline void plus(const MatrixBase<D1>& d1, const MatrixBase<D2>& d2, MatrixBase<D3>& d_plus)
902{
906
907 const auto p1 = d1.template head<2>();
908 const auto p2 = d2.template head<2>();
909 Map<Matrix<typename D3::Scalar, 2, 1>> p_p(&d_plus(0));
910
911 plus(p1, d1(2), p2, d2(2), p_p, d_plus(2));
912}
913
916template <typename D1, typename D2>
917inline Matrix<typename D1::Scalar, 3, 1> plus(const MatrixBase<D1>& d1, const MatrixBase<D2>& d2)
918{
919 Matrix<typename D1::Scalar, 3, 1> d_plus;
920 plus(d1, d2, d_plus);
921 return d_plus;
922}
923
926inline void plus(const VectorComposite& x, const VectorComposite& tau, VectorComposite& res)
927{
928 res['P'].resize(2);
929 res['O'].resize(1);
930 plus(x.at('P'), x.at('O'), tau.at('P'), tau.at('O'), res['P'], res['O']);
931}
932
936{
937 VectorComposite res;
938 plus(x, tau, res);
939 return res;
940}
941
943// MINUS (block-wise)
944
947template <typename D1, typename D2, typename D3>
948inline void minus(const MatrixBase<D1>& p1,
949 const typename D1::Scalar& o1,
950 const MatrixBase<D2>& p2,
951 const typename D2::Scalar& o2,
952 MatrixBase<D3>& pd,
953 typename D3::Scalar& od)
954{
958
959 pd = p2 - p1;
960 od = pi2pi(o2 - o1);
961}
962
965template <typename D1, typename D2, typename D3>
966inline void minus(const MatrixBase<D1>& p1,
967 const typename D1::Scalar& o1,
968 const MatrixBase<D2>& p2,
969 const typename D2::Scalar& o2,
970 MatrixBase<D3>& pd,
971 typename D3::Scalar& od,
972 typename D3::Scalar& J_do_o1,
973 typename D3::Scalar& J_do_o2)
974{
975 minus(p1, o1, p2, o2, pd, od);
976
977 J_do_o1 = typename D3::Scalar(-1);
978 J_do_o2 = typename D3::Scalar(1);
979}
980
983template <typename D1, typename D2, typename D4, typename D5, typename D7, typename D8>
984inline void minus(const MatrixBase<D1>& p1,
985 const MatrixBase<D2>& o1,
986 const MatrixBase<D4>& p2,
987 const MatrixBase<D5>& o2,
988 MatrixBase<D7>& pd,
989 MatrixBase<D8>& od)
990{
994
995 minus(p1, o1(0), p2, o2(0), pd, od(0));
996}
997
1000template <typename D1, typename D2, typename D3>
1001inline void minus(const MatrixBase<D1>& d1, const MatrixBase<D2>& d2, MatrixBase<D3>& err)
1002{
1006
1007 const auto p1 = d1.template head<2>();
1008 const auto p2 = d2.template head<2>();
1009 Map<Matrix<typename D3::Scalar, 2, 1>> pd(&err(0));
1010
1011 minus(p1, d1(2), p2, d2(2), pd, err(2));
1012}
1013
1016template <typename D1, typename D2, typename D3, typename D4, typename D5>
1017inline void minus(const MatrixBase<D1>& d1,
1018 const MatrixBase<D2>& d2,
1019 MatrixBase<D3>& dif,
1020 MatrixBase<D4>& J_diff_d1,
1021 MatrixBase<D5>& J_diff_d2)
1022{
1025
1026 minus(d1, d2, dif);
1027
1028 // block-wise difference: trivial jacobians
1029 J_diff_d1 = -Matrix<typename D4::Scalar, 3, 3>::Identity();
1030 J_diff_d2 = Matrix<typename D5::Scalar, 3, 3>::Identity();
1031}
1032
1035template <typename D1, typename D2>
1036inline Matrix<typename D1::Scalar, 3, 1> minus(const MatrixBase<D1>& d1, const MatrixBase<D2>& d2)
1037{
1038 Matrix<typename D1::Scalar, 3, 1> ret;
1039 minus(d1, d2, ret);
1040 return ret;
1041}
1042
1045inline void minus(const VectorComposite& x1, const VectorComposite& x2, VectorComposite& res)
1046{
1047 res['P'].resize(2);
1048 res['O'].resize(1);
1049 minus(x1.at('P'), x1.at('O'), x2.at('P'), x2.at('O'), res['P'], res['O']);
1050}
1051
1055{
1056 VectorComposite res;
1057 minus(x1, x2, res);
1058 return res;
1059}
1060
1062// INTERPOLATE
1063
1066template <typename D1, typename D2, typename D3>
1067inline void interpolate(const MatrixBase<D1>& d1,
1068 const MatrixBase<D2>& d2,
1069 const typename D1::Scalar& t,
1070 MatrixBase<D3>& ret)
1071{
1075
1076 Matrix<typename D1::Scalar, 3, 1> dd = between(d1, d2);
1077
1078 Matrix<typename D1::Scalar, 3, 1> tau = t * log(dd);
1079
1080 ret = compose(d1, exp(tau));
1081}
1082
1085template <typename D1, typename D2>
1086inline Matrix<typename D1::Scalar, 3, 1> interpolate(const MatrixBase<D1>& d1,
1087 const MatrixBase<D2>& d2,
1088 const typename D1::Scalar& t)
1089{
1090 Matrix<typename D1::Scalar, 3, 1> ret;
1091 interpolate(d1, d2, t, ret);
1092 return ret;
1093}
1094
1097inline void interpolate(const VectorComposite& x1, const VectorComposite& x2, const double& t, VectorComposite& res)
1098{
1099 VectorComposite tau = log(between(x1, x2));
1100 tau['P'] *= t;
1101 tau['O'] *= t;
1102 compose(x1, exp(tau), res);
1103}
1104
1107inline VectorComposite interpolate(const VectorComposite& x1, const VectorComposite& x2, const double& t)
1108{
1109 VectorComposite res;
1110 interpolate(x1, x2, t, res);
1111 return res;
1112}
1113
1115// HOMOGENEOUS MATRIX
1116
1119template <typename D1, typename D2>
1120inline void toSE2(const MatrixBase<D1>& pose, MatrixBase<D2>& SE2)
1121{
1124
1125 typedef typename D1::Scalar T;
1126
1127 SE2.template block<2, 1>(0, 2) = pose.template head<2>();
1128 SE2.template block<2, 2>(0, 0) = SO2::exp(T(pose(2)));
1129 SE2.template block<1, 2>(2, 0).setZero();
1130 SE2(2, 2) = (T)1.0;
1131}
1132
1135template <typename D>
1136inline Matrix<typename D::Scalar, 3, 3> toSE2(const MatrixBase<D>& pose)
1137{
1138 Matrix<typename D::Scalar, 3, 3> SE2;
1139 toSE2(pose, SE2);
1140 return SE2;
1141}
1142
1143} // namespace SE2
1144} // namespace wolf
Composite of state vectors.
Definition: vector_composite.h:29
Namespace extending Eigen definitions.
Definition: wolf.h:91
const double EPS
Definition: wolf.h:74
Matrix< typename D::Scalar, 3, 3 > jac_SE2_left(const MatrixBase< D > &tangent)
Left Jacobian of SE(2), with tangent = [rho, theta]. See [1] eq. 164.
Definition: SE2.h:185
void identity(MatrixBase< D > &p, typename D::Scalar &o)
Identity delta (by parts, with the angle as a scalar)
Definition: SE2.h:263
Matrix< T, 2, 2 > V_helper(const T theta)
V(theta) matrix, so that exp([rho, theta]) = [V(theta) * rho, theta]. See [1] eq. 158.
Definition: SE2.h:107
void exp(const MatrixBase< D1 > &rho, const typename D1::Scalar &theta, MatrixBase< D2 > &p, typename D2::Scalar &o)
Exponential map: from the tangent vector [rho, theta] to the delta (by parts, with the angle as a sca...
Definition: SE2.h:659
void compose(const MatrixBase< D1 > &p1, const typename D1::Scalar &o1, const MatrixBase< D2 > &p2, const typename D2::Scalar &o2, MatrixBase< D3 > &pc, typename D3::Scalar &oc)
Composition of two deltas: d1 (+) d2 (by parts, with the angle as a scalar)
Definition: SE2.h:393
Matrix< typename D::Scalar, 3, 3 > jac_SE2_right_inv(const MatrixBase< D > &tangent)
Inverse of the right Jacobian of SE(2), with tangent = [rho, theta].
Definition: SE2.h:252
void log(const MatrixBase< D1 > &p, const typename D1::Scalar &o, MatrixBase< D2 > &rho, typename D2::Scalar &theta)
Logarithmic map: from the delta to the tangent vector [rho, theta] (by parts, with the angle as a sca...
Definition: SE2.h:773
void between(const MatrixBase< D1 > &p1, const typename D1::Scalar &o1, const MatrixBase< D2 > &p2, const typename D2::Scalar &o2, MatrixBase< D3 > &p12, typename D3::Scalar &o12)
Delta between two deltas: d1.inv (+) d2 (by parts, with the angle as a scalar)
Definition: SE2.h:577
void toSE2(const MatrixBase< D1 > &pose, MatrixBase< D2 > &SE2)
Homogeneous 3x3 matrix of a pose.
Definition: SE2.h:1120
Matrix< typename D::Scalar, 3, 3 > jac_SE2_right(const MatrixBase< D > &tangent)
Right Jacobian of SE(2), with tangent = [rho, theta]. See [1] eq. 163.
Definition: SE2.h:228
void minus(const MatrixBase< D1 > &p1, const typename D1::Scalar &o1, const MatrixBase< D2 > &p2, const typename D2::Scalar &o2, MatrixBase< D3 > &pd, typename D3::Scalar &od)
Block-wise minus: [p2 - p1, o2 - o1] (by parts, with the angle as a scalar)
Definition: SE2.h:948
void interpolate(const MatrixBase< D1 > &d1, const MatrixBase< D2 > &d2, const typename D1::Scalar &t, MatrixBase< D3 > &ret)
Interpolation of two deltas: d1 (+) exp(t * log(d1.inv (+) d2)) (full vectors)
Definition: SE2.h:1067
Matrix< typename D::Scalar, 3, 3 > jac_SE2_left_inv(const MatrixBase< D > &tangent)
Inverse of the left Jacobian of SE(2), with tangent = [rho, theta].
Definition: SE2.h:236
void inverse(const MatrixBase< D1 > &p, const typename D1::Scalar &o, MatrixBase< D2 > &ip, typename D2::Scalar &io)
Inverse delta (by parts, with the angle as a scalar)
Definition: SE2.h:325
Matrix< T, 2, 2 > skew(const T &t)
Skew-symmetric matrix of a scalar: [0, -t; t, 0].
Definition: SE2.h:91
VectorComposite identityComposite()
Returns the identity delta, as a VectorComposite with keys 'P' and 'O'.
Definition: SE2.h:312
Matrix< typename D::Scalar, 2, 1 > skewed(const MatrixBase< D > &t)
Computes [1]_x * t = (-t.y ; t.x)
Definition: SE2.h:99
Matrix< T, 2, 1 > J_Vp_theta(const MatrixBase< D > &p, const T theta)
Jacobian of V(theta)*p wrt theta.
Definition: SE2.h:149
Matrix< T, 2, 2 > V_inv_helper(const T theta)
Inverse of V(theta), so that log([p, theta]) = [V(theta).inv * p, theta].
Definition: SE2.h:133
void plus(const MatrixBase< D1 > &p1, const typename D1::Scalar &o1, const MatrixBase< D2 > &p2, const typename D2::Scalar &o2, MatrixBase< D3 > &plus_p, typename D3::Scalar &plus_o)
Block-wise plus: [p1 + p2, o1 + o2] (by parts, with the angle as a scalar)
Definition: SE2.h:866
Matrix< T, 2, 2 > exp(const T theta)
Rotation matrix of an angle.
Definition: SE2.h:77
Jet< T, N > abs(const Jet< T, N > &f)
Definition: jet.h:531
Jet< T, N > sin(const Jet< T, N > &f)
Definition: jet.h:641
Jet< T, N > tan(const Jet< T, N > &f)
Definition: jet.h:656
Jet< T, N > cos(const Jet< T, N > &f)
Definition: jet.h:626
Definition: capture_base.h:23
T pi2pi(const T &_angle)
Returns the angle wrapped to [-pi, pi].
Definition: rotations.h:33
static void check(const Eigen::MatrixBase< T > &t)
Asserts the matrix size dynamically or statically.
Definition: eigen_assert.h:103